package com.schoolhelper.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "file.upload")
public class FileUploadConfig {
    
    private String path;
    private String baseUrl;
    private String allowedTypes;
    private long maxSize;
    
    // Getters and Setters
    public String getPath() {
        return path;
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    
    public String getAllowedTypes() {
        return allowedTypes;
    }
    
    public void setAllowedTypes(String allowedTypes) {
        this.allowedTypes = allowedTypes;
    }
    
    public long getMaxSize() {
        return maxSize;
    }
    
    public void setMaxSize(long maxSize) {
        this.maxSize = maxSize;
    }
    
    /**
     * 检查文件类型是否被允许
     */
    public boolean isAllowedFileType(String fileExtension) {
        if (allowedTypes == null || fileExtension == null) {
            return false;
        }
        String[] types = allowedTypes.toLowerCase().split(",");
        String ext = fileExtension.toLowerCase().replace(".", "");
        for (String type : types) {
            if (type.trim().equals(ext)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取完整的文件访问URL
     */
    public String getFileUrl(String relativePath) {
        if (relativePath == null || relativePath.isEmpty()) {
            return null;
        }
        // 确保相对路径以/开头
        if (!relativePath.startsWith("/")) {
            relativePath = "/" + relativePath;
        }
        return baseUrl + relativePath;
    }
}